/* Copyright (C) 2006 - 2009 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/* ScriptData
SDName: Boss Lurker
SD%Complete: 80
SDComment: For later event
SDCategory: Bosses
EndScriptData */

#include "ScriptPCH.h"

enum eEnums
{
    SAY_AGGRO               = -1000360,

    SPELL_SLEEP             = 24777,
    SPELL_NOXIOUSBREATH     = 24818,
    SPELL_TAILSWEEP         = 15847,
    SPELL_LIGHTNINGWAVE     = 24819,
    SPELL_MOONFIRE          = 21669
};

const uint32 ServantSpawns[6] =
{
    18470, 16805, 21242, 18452, 22482, 21285
};

class boss_lurker : public CreatureScript
{
public:
    boss_lurker() : CreatureScript("boss_lurker") { }

	struct boss_lurkerAI : public ScriptedAI
	{
		boss_lurkerAI(Creature* pCreature) : ScriptedAI(pCreature) {}

		uint32 m_uiNoxiousBreath_Timer;
		uint32 m_uiTailSweep_Timer;
		uint32 m_uiLightningWave_Timer;

		void Reset()
		{
			m_uiNoxiousBreath_Timer = 8000;
			m_uiTailSweep_Timer = 4000;
			m_uiLightningWave_Timer = 12000;
		}

		void DamageTaken(Unit *done_by, uint32 &damage)
		{
			if ((me->GetHealth()-damage)*100 / me->GetMaxHealth() < 30)

				if (rand()%100 < 30)
				{
					me->SummonCreature(ServantSpawns[rand()%6], 0.0f, 0.0f, 0.0f,0.0f, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT, 60000);
					me->SummonCreature(ServantSpawns[rand()%6], 0.0f, 0.0f, 0.0f, 0.0f, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT, 60000);
				}
		}
					
		void EnterCombat(Unit* pWho)
		{
			DoScriptText(SAY_AGGRO, me);
		}

		void MoveInLineOfSight(Unit *who) { }	
	
		void UpdateAI(const uint32 uiDiff)
		{
			if (!UpdateVictim())
				return;

			if (m_uiNoxiousBreath_Timer <= uiDiff)
			{
				DoCast(me->getVictim(), SPELL_NOXIOUSBREATH);
				m_uiNoxiousBreath_Timer = 14000 + rand()%6000;
			}
			else
				m_uiNoxiousBreath_Timer -= uiDiff;

			if (m_uiTailSweep_Timer <= uiDiff)
			{
				if (Unit *pTarget = SelectUnit(SELECT_TARGET_RANDOM, 0))
					DoCast(pTarget, SPELL_TAILSWEEP);

				m_uiTailSweep_Timer = 2000;
			}
			else
				m_uiTailSweep_Timer -= uiDiff;

			if (m_uiLightningWave_Timer <= uiDiff)
			{
				if (Unit *pTarget = SelectUnit(SELECT_TARGET_RANDOM, 0))
					DoCast(pTarget, SPELL_LIGHTNINGWAVE);

				m_uiLightningWave_Timer = 7000 + rand()%5000;
			}
			else
				m_uiLightningWave_Timer -= uiDiff;
				DoMeleeAttackIfReady();
		}
	};

	CreatureAI* GetAI(Creature* pCreature)
	{
		return new boss_lurkerAI (pCreature);
	}
};

void AddSC_boss_lurker()
{
	new boss_lurker;
}